package me.luger.project.fof.controller.admin;

import me.luger.core.commom.util.ArrayUtil;
import me.luger.core.commom.util.LogUtil;
import me.luger.core.commom.util.StringUtil;
import me.luger.core.dao.nutz.entity.page.DataTableRequest;
import me.luger.core.mvc.controller.BaseController;
import me.luger.core.mvc.entity.Result;
import me.luger.core.mvc.interceptor.oplog.OpLog;
import me.luger.project.fof.service.AssetStrategyInfoService;
import me.luger.project.fof.service.ProductTypeService;
import me.luger.project.fof.entity.AssetStrategyInfo;
import me.luger.project.fof.entity.ProductType;
import org.nutz.dao.Cnd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * ProductType controller
 */
@Controller
@RequestMapping("/private/fof/productType")
public class ProductTypeController extends BaseController {

    private String viewBasePath = "/admin/modules/productType/";

    @Autowired
    private ProductTypeService productTypeService;

    @Autowired
    private AssetStrategyInfoService assetStrategyInfoService;

    @RequestMapping(value = "",name = "ProductType列表页面")
    public ModelAndView index(){
        return btlView(viewBasePath + "index");
    }

    @RequestMapping(value = "data",name = "获取ProductType列表")
    @ResponseBody
    public Object data(@RequestBody final DataTableRequest dataTableRequest){
        return productTypeService.data(dataTableRequest);
    }



    @RequestMapping(value = "delete",name = "删除ProductType接口")
    @ResponseBody
    public Object delete(String[] ids){
        productTypeService.delete(ids);
        return Result.success("delete success");
    }

    @RequestMapping(value = "/detail/{id}",name = "获取ProductType详细信息接口")
    public ModelAndView detail(@PathVariable("id") Integer id){
        ProductType productType = productTypeService.fetchById(id);
        String assetStrategyIds = productType.getAssetStrategyIds();
        List<AssetStrategyInfo> assetStrategyInfoList = null;
        if(!StringUtil.isBlank(assetStrategyIds)){
            if(assetStrategyIds.startsWith(",")){
               assetStrategyIds = assetStrategyIds.substring(1);
            }
            if(assetStrategyIds.endsWith(",")){
                assetStrategyIds = assetStrategyIds.substring(0,assetStrategyIds.length()-1);
            }
            assetStrategyInfoList = assetStrategyInfoService.list(Cnd.where("id","in",assetStrategyIds));
        }
        setAttr("assetStrategyInfoList",assetStrategyInfoList);
        setAttr("productType",productType);
        return btlView(viewBasePath + "detail");
    }

    @RequestMapping(value = "add",name = "添加ProductType页面")
    public ModelAndView add(){
        List<AssetStrategyInfo> assetInfoList = assetStrategyInfoService.getAssetStrategyInfoByType(1);
        List<AssetStrategyInfo> strategyInfoList = assetStrategyInfoService.getAssetStrategyInfoByType(2);

        setAttr("assetInfoList",assetInfoList);
        setAttr("strategyInfoList",strategyInfoList);
        return btlView(viewBasePath + "add");
    }

    @RequestMapping(value = "addDo",name = "添加ProductType接口")
    @ResponseBody
    @OpLog(name = "添加ProductType",msg = "添加ProductType")
    public Object adddo(ProductType productType){
        try{
            String[] assetStrategyArr = request.getParameterValues("assetStrategyArr");
            if(productType.getRcm() == null){
                productType.setRcm(0);
            }
            String assetStrategy = ArrayUtil.toStringWithoutBrackets(assetStrategyArr);
            productType.setAssetStrategyIds(","+assetStrategy+",");
            productType = productTypeService.insert(productType);
        }catch (Exception e){
            LogUtil.errorLog(e,"adddo error");
            return Result.error();
        }
        return Result.success();
    }

    @RequestMapping(value = "/edit/{id}",name = "更新ProductType信息页面")
    public ModelAndView edit(@PathVariable Integer id){
        ProductType productType = productTypeService.fetchById(id);
        setAttr("productType",productType);

        List<AssetStrategyInfo> assetInfoList = assetStrategyInfoService.getAssetStrategyInfoByType(1);
        List<AssetStrategyInfo> strategyInfoList = assetStrategyInfoService.getAssetStrategyInfoByType(2);

        setAttr("assetInfoList",assetInfoList);
        setAttr("strategyInfoList",strategyInfoList);
        return btlView(viewBasePath + "edit");
    }

    @RequestMapping(value = "editDo",name = "更新ProductType接口")
    @ResponseBody
    public Object editDo(ProductType productType){
        try{
            String[] assetStrategyArr = request.getParameterValues("assetStrategyArr");
            if(productType.getRcm() == null){
                productType.setRcm(0);
            }
            String assetStrategy = ArrayUtil.toStringWithoutBrackets(assetStrategyArr);
            productType.setAssetStrategyIds(","+assetStrategy+",");
            productTypeService.updateIgnoreNull(productType);
        }catch (Exception e){
            return Result.error();
        }
        return Result.success();
    }
}
